package com.example.kai.uno.Ranking;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.kai.uno.MyDBHelper;
import com.example.kai.uno.R;


public class MyFragment4 extends Fragment {
    private static final String TAG = "MyFragment4";
    String b[];
    String []c={"暂时没有新数据"};
    private MyDBHelper dbHelper;
    public String[] getData(){
        dbHelper = new MyDBHelper(getActivity(),"UserStore.db",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int bd=0;

        String string ="";
        Cursor cursor =db.rawQuery("select * from scoreData3 order by score",null);
        if( cursor.getCount()==0)
        {b=c;
            return b;}
        else{
            b=new String[cursor.getCount()];
            if(cursor.moveToFirst())
            {int a=1;
                do {
                    string +=a+":"+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getInt(cursor.getColumnIndex("score"))+" "+cursor.getString(cursor.getColumnIndex("time"));
                    b[bd]=string;
                    string="";
                    a++;
                    bd++;
                }while(cursor.moveToNext());
            }
            cursor.close();
            return b;
        }}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listview_content, container, false);
        ListView list_content = (ListView) view.findViewById(R.id.list_content);


        dbHelper = new MyDBHelper(getActivity(),"UserStore.db",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cr = db.query("scoreData3", null, null, null, null, null, "score asc");
        String[] ColumnNames = {"_id","score","time"};

        ListAdapter adapter = new MySimpleCursorAdapter(getActivity(), R.layout.listviewlayout, cr, ColumnNames, new int[] { R.id.name, R.id.score, R.id.time });

        list_content.setAdapter(adapter);

        Log.i(TAG, "onCreateView: 0000--------MyFragment1");
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: 0000-------MyFragment4");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: 0000--------MyFragment4");
    }

    public MyFragment4() {
    }

}
